using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;

public partial class report_Default : System.Web.UI.Page
{
    public static string AUTHENTICATE_SESSION_ID = "AUTH_DATA";
    public readonly static string IMAGE_FILE_EXTENTIONS = ".jpg .jpeg .jpe .png .gif .bmp";
    public Patient _Patient = null;
    public List<string> _Images = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!this.IsAuthenticated)
            {
                Response.Redirect("Logout.aspx", true);
                return;
            }

            string localPath = Server.MapPath("/" + _Patient.FolderPath);
            this.LoadImages(localPath);
        }
        catch { return; }
    }

    public void LoadImages(string path)
    {
        try
        {
            _Images = new List<string>();

            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path);
                if (files != null && files.Length > 0)
                {
                    //Response.Write("<br />Files: " + string.Join("<br />", files));

                    foreach (string file in files)
                    {
                        if (this.IsImageFile(file))
                        {
                            int indexOf = file.IndexOf("\\report\\", StringComparison.OrdinalIgnoreCase);

                            if (indexOf < 0)
                            {
                                indexOf = file.IndexOf("/report/", StringComparison.OrdinalIgnoreCase);
                            }

                            if (indexOf >= 0)
                            {
                                _Images.Add(file.Substring(indexOf).Replace("\\", "/"));
                            }
                        }
                    }
                }
                else
                {
                    //Response.Write("<br />No file found.");
                }
            }
            else
            {
                //Response.Write("<br />Not exist");
            }
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message + "<br /><br />" + ex.StackTrace);
        }
    }

    private bool IsImageFile(string file)
    {
        if (file == null)
        {
            return false;
        }

        string ex = Path.GetExtension(file);
        return IMAGE_FILE_EXTENTIONS.IndexOf(ex, StringComparison.OrdinalIgnoreCase) >= 0;
    }

    private bool IsAuthenticated
    {
        get
        {
            try
            {
                if (Session[AUTHENTICATE_SESSION_ID] != null)
                {
                    _Patient = (Patient)Session[AUTHENTICATE_SESSION_ID];
                    return true;
                }
            }
            catch { }
            Session.Remove(AUTHENTICATE_SESSION_ID);
            return false;
        }
    }
}